// 144. 二叉树的前序遍历
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
 var preorderTraversal = function(root) {
    const res=[];
    const stack=[root];
    let cur=null;
    if(!root) return res;
    while(stack.length){
        cur=stack.pop();
        res.push(cur.val);
        cur.right&&stack.push(cur.right);
        cur.left&&stack.push(cur.left);
    }
    return res;
};

// 145. 二叉树的后序遍历
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
 var postorderTraversal = function(root) {
    const res=[];
    const stack=[root];
    let cur=null;
    if(!root) return res;
    do{
        cur=stack.pop();
        res.push(cur.val);
        cur.left&&stack.push(cur.left);
        cur.right&&stack.push(cur.right);
    }while(stack.length)
    res.reverse();
    return res;
};

// 94. 二叉树的中序遍历
/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
 var inorderTraversal = function(root) {
    const res=[];
    const stack=[];
    let cur=root;
    while(stack.length||cur){
        if(cur){
            stack.push(cur);
            cur=cur.left;
        }else{
            cur=stack.pop();
            res.push(cur.val);
            cur=cur.right;
        }
        
    }
    return res;
};